package org.gogenie.android.ui;

import java.util.HashMap;
import java.util.Map;

import org.gogenie.android.model.Post;

import uy.com.silveira.noname.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Show the user back his picture and get description and location.
 * @author jjso
 */
public class CategorySelectionActivity extends Activity {

  public static String EXTRA_POST = "Post";

  public static Map<Integer, Integer[]> buttonImages;

  static {
    buttonImages = new HashMap<Integer, Integer[]>();
    buttonImages.put(R.id.loved_btn, new Integer[]{R.drawable.heart, R.drawable.heart_bw});
    buttonImages.put(R.id.hated_btn, new Integer[]{R.drawable.thumbs_down, R.drawable.thumbs_down_bw});
    buttonImages.put(R.id.spotted_btn, new Integer[]{R.drawable.spotted, R.drawable.spotted_bw});
  }

  private Post post;

  public CategorySelectionActivity() {
    super();
  }

  private void swapButtons(View view) {
    Button button = (Button)view;
    for(Integer buttonId : buttonImages.keySet()) {
      if (buttonId == button.getId()) {
        button.setBackgroundResource(buttonImages.get(buttonId)[0]);
      } else {
        Button deselectedButton = (Button) findViewById(buttonId);
        deselectedButton.setBackgroundResource(buttonImages.get(buttonId)[1]);
      }
    }
  }

  private OnClickListener categorySelectionClickListener = new OnClickListener() {
    public void onClick(View v) {
      swapButtons(v);
    }
  };

  private OnClickListener saveAndContinueClickListener = new OnClickListener() {
    public void onClick(View v) {
      openAwesome();
    }
  };

  private void openAwesome() {
    Intent openAwesome = new Intent(this, AwesomeActivity.class);
    startActivity(openAwesome);
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_selection);
    for(Integer buttonId : buttonImages.keySet()) {
      ((Button) findViewById(buttonId)).setOnClickListener(
          categorySelectionClickListener);
    }
    ((Button) findViewById(R.id.send_btn)).setOnClickListener(
        saveAndContinueClickListener);

    post = (Post) getIntent().getSerializableExtra(EXTRA_POST);
    showStuff();
  }

  private void showStuff() {
    if (post.getImagePath() != null) {
      ImageView imageViewer = (ImageView)findViewById(R.id.image_viewer);
      imageViewer.setImageDrawable(BitmapDrawable.createFromPath(post.getImagePath()));
    }

    TextView description = (TextView)findViewById(R.id.description_text);
    description.setText(post.getDescription());
  }
}
